import $ from './library/jquery.js';
import './library/niceCountryInput.js';

$('.lang').on('click', function(e) {
    $('.lang_w').removeClass('none');
    e = e || window.event;
    if (e.cancelBubble) {
        e.cancelBubble = true;
    } else {
        e.stopPropagation();
    }
});

$('body').on('click', function() {
    $('.lang_w').addClass('none');
})

$('.eyes').on('click', function() {
    $(this).addClass('none').siblings().removeClass('none');
    $('#password').attr('type', 'text');
})

$('.eyel').on('click', function() {
    $(this).addClass('none').siblings().removeClass('none');
    $('#password').attr('type', 'password');
})

// $('.submit').on('click', function () {
//     $.ajax({
//         type: "get",
//         url: "../../interface/reg.php",
//         data: {
//             username: $('#username').val(),
//             password: $('#password').val()
//         },
//         dataType: "json"
//     }).then(function (res) {
//         if(res.has) {
//             alert(res.msg);
//         } else{
//             alert(res.msg);
//             location.href="../html/login.html"
//         }
//     }).catch(function (xhr) {
//         console.log(xhr.status);
//     });
// });


$('.lognn').on('click', function() {
    // console.log($('.username').val())
    // console.log($('.password').val())
    $.ajax({
        type: "get",
        url: "../../interface/login.php",
        data: {
            username: $('.username').val(),
            password: $('.password').val()
        },
        dataType: "json"
    }).then(function(res) {
        if (res.has) {
            alert(res.msg);
            location.href = "../html/index.html"
        } else {
            alert(res.msg);
            location.href = "../html/login.html"
        }
    }).catch(function(xhr) {
        console.log(xhr.status);
    });
});


$('.pas-login').on('click', function() {
    $('.login_password').removeClass('none').prev().addClass('none');
});


$('.dx-login').on('click', function() {
    if ($('.login_Yanz').hasClass('none')) {
        $('.login_Yanz').removeClass('none').next().addClass('none');
        return
    }
});


$('.sao_ma').on('click', function() {
    $('.login').addClass('none').next().removeClass('none');
    $('.pc_ma').removeClass('none').prev().addClass('none');
})


$('.pc_ma').on('click', function() {
    $('.login').removeClass('none').next().addClass('none');
    $('.sao_ma').removeClass('none').next().addClass('none');
})



let reg = [
    /^.{6,16}$/, // 验证6-16
    /[A-Za-z]+/, // 验证字母
    /\d+/, // 验证数字
    /[^A-Za-z0-9]+/ // 验证特殊符号 
];

$('#password').on('change', function() {

    let check = reg.map(el => el.test(this.value));
    let len = check.shift();
    let strong = check.reduce((prev, next) => prev + next);

    if (len) {
        if (strong > 1) {
            $('.submit').on('click', function() {
                $.ajax({
                    type: "get",
                    url: "../../interface/reg.php",
                    data: {
                        username: $('#username').val(),
                        password: $('#password').val()
                    },
                    dataType: "json"
                }).then(function(res) {
                    if (res.has) {
                        alert(res.msg);
                        location.reload();
                    } else {
                        alert(res.msg);
                        location.href = "../html/login.html"
                    }
                }).catch(function(xhr) {
                    console.log(xhr.status);
                });
            });

        } else {
            alert('您输入的密码不符合规则，请重新输入');
        }
    } else {
        alert('您输入的密码长度不符，请重新输入');
    }
});

$('.eyess').on('click', function() {
    $(this).addClass('none').siblings().removeClass('none');
    $('.password').attr('type', 'text');
})

$('.eyell').on('click', function() {
    $(this).addClass('none').siblings().removeClass('none');
    $('.password').attr('type', 'password');
})

//国家下拉
function onChangeCallback(ctr) {
    console.log("The country was changed: " + ctr);
}
$(document).ready(function() {
    new NiceCountryInput($("#testinput")).init();
});